package com.yh.jandan.http ;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class HttpUtil {

	private String _CHARSET = "UTF-8" ;
	private HttpClient httpclient = null ;
	private Map<String, String> header = new HashMap<String, String> ( ) ;

	public HttpUtil ( ) {
		this ( "UTF-8" ) ;
	}

	public HttpUtil ( String charset ) {
		this._CHARSET = charset ;
		initHeader ( ) ;
		httpclient = buildHttpClint ( ) ;
		// httpclient.getParams().setIntParameter("http.socket.timeout", 10000);
	}

	// public HttpUtil ( String charset , HttpHost proxy ) {
	// this ( charset );
	// addProxy ( proxy );
	// }

	public void addProxy ( HttpHost proxy ) {
		httpclient.getParams ( ).setParameter ( ConnRoutePNames.DEFAULT_PROXY , proxy ) ;
	}

	public Resp POST ( String url , Map<String, String> headers , Map<String, String> params ) throws ClientProtocolException, IOException {
		HttpPost _post = buildPostReq ( url ) ;
		if ( null != headers ) for ( String k : headers.keySet ( ) ) {
			_post.addHeader ( k , headers.get ( k ) ) ;
		}
		if ( null != params && params.size ( ) > 0 ) {
			List<NameValuePair> formparams = new ArrayList<NameValuePair> ( ) ;
			for ( String p : params.keySet ( ) ) {
				formparams.add ( new BasicNameValuePair ( p , params.get ( p ) ) ) ;
			}
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity ( formparams , _CHARSET ) ;
			_post.setEntity ( entity ) ;
		}
		HttpResponse response = httpclient.execute ( _post ) ;
		Resp resp = buildResp ( response ) ;
		// U.print( "POST[" + resp.getStatusCode() + "]:" + url );
		return resp ;
	}

	// public static void main( String[] args ) throws ClientProtocolException,
	// IOException {
	// HttpUtil httpUtil = new HttpUtil();
	// String url = "http://jandan.net/ooxx";
	// Resp resp = httpUtil.GET( url , null );
	// System.out.println( resp.getHtml() );
	// }

	public Resp GET ( String url , Map<String, String> headers ) throws ClientProtocolException, IOException {
		HttpGet _get = buildGetReq ( url ) ;
		if ( null != headers ) for ( String k : headers.keySet ( ) ) {
			_get.addHeader ( k , headers.get ( k ) ) ;
		}
		HttpResponse response = httpclient.execute ( _get ) ;
		Resp resp = buildResp ( response ) ;
		// U.print( "GET[" + resp.getStatusCode() + "]:" + url );
		return resp ;
	}

	public void close ( ) {
		httpclient.getConnectionManager ( ).shutdown ( ) ;
	}

	private Resp buildResp ( HttpResponse response ) throws IllegalStateException, IOException {
		String result = null ;
		int statusCode = response.getStatusLine ( ).getStatusCode ( ) ;
		HttpEntity entity = response.getEntity ( ) ;
		if ( entity != null ) {
			InputStream instream = entity.getContent ( ) ;
			result = IOUtils.toString ( instream , _CHARSET ) ;
		}
		return new Resp ( statusCode , result , response ) ;
	}

	private HttpPost buildPostReq ( String url ) {
		HttpPost _post = new HttpPost ( url ) ;
		if ( null != header && header.size ( ) > 0 ) for ( String h : header.keySet ( ) ) {
			_post.addHeader ( h , header.get ( h ) ) ;
		}
		return _post ;
	}

	private HttpGet buildGetReq ( String url ) {
		HttpGet _get = new HttpGet ( url ) ;
		if ( null != header && header.size ( ) > 0 ) for ( String h : header.keySet ( ) ) {
			_get.addHeader ( h , header.get ( h ) ) ;
		}
		return _get ;
	}

	private HttpClient buildHttpClint ( ) {
		return new DefaultHttpClient ( ) ;
	}

	public String getCharset ( ) {
		return _CHARSET ;
	}

	private void initHeader ( ) {
		header.put ( "Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ) ;
		header.put ( "Accept-Language" , "zh-CN,zh;q=0.8" ) ;
		header.put ( "Cache-Control" , "no-cache" ) ;
		header.put ( "Connection" , "keep-alive" ) ;
		header.put ( "Pragma" , "no-cache" ) ;
		header.put ( "Upgrade-Insecure-Requests" , "1" ) ;
		header.put ( "User-Agent" , "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" ) ;
	}

}
